package com.zhoug.zmui3.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.zmui3.R;
import com.zhoug.zmui3.ints.IDismissListenerWindow;
import com.zhoug.zmui3.ints.OnDismissListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.FragmentManager;

/**
 * @Author 35574
 * @Date 2020/10/15
 * @Description
 */
public abstract class ZMUIAbsBottomSheetDialogFragment extends BottomSheetDialogFragment implements IDismissListenerWindow {
    private static final String TAG = ">>>ZMUIAbsBottomSheetDialogFragment";
    private static final boolean DEBUG=false;
    /**
     * 关闭窗口监听
     */
    private List<OnDismissListener> onDismissListeners;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (transparentBackground()) {
            if (getContext() == null) {
                return super.onCreateDialog(savedInstanceState);
            }
            return new BottomSheetDialog(this.getContext(), R.style.zmui_transparentBottomSheetStyle);
        } else {
            return super.onCreateDialog(savedInstanceState);
        }
    }

    /**
     * 设置系统设置的默认背景透明,只有设置透明后,我们在布局文件中定义的圆角背景才能显示
     *
     * @return
     */
    protected boolean transparentBackground() {
        return false;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResId(), container, false);
        init(view);
        return view;
    }

    protected abstract @LayoutRes
    int getLayoutResId();

    protected abstract void init(View root);

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            //设置遮罩透明
            float dimAmount = getDimAmount();
            if (dimAmount >= 0) {
                Window window = dialog.getWindow();
                if (window != null) {
                    WindowManager.LayoutParams attributes = window.getAttributes();
                    attributes.dimAmount = dimAmount;
                    window.setAttributes(attributes);
                }
            }
            //设置高度
            final int height = getHeight();
            if (height > 0) {
                //设置bottomSheet高度
                View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
                if (bottomSheet != null && bottomSheet.getLayoutParams() != null) {
                    bottomSheet.getLayoutParams().height = height;
                }
                final View view = getView();
                if (view != null) {
                    view.post(() -> {
                        View parent = (View) view.getParent();
                        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                        CoordinatorLayout.Behavior behavior = params.getBehavior();
                        BottomSheetBehavior mBottomSheetBehavior = (BottomSheetBehavior) behavior;
                        //设置高度
                        if (mBottomSheetBehavior != null) {
                            mBottomSheetBehavior.setPeekHeight(height);
                        }
                    });
                }
            }
        }
    }

    /**
     * <0:默认值
     *
     * @return
     */
    protected int getHeight() {
        //设置bottomSheet高度
        return -1;
    }

    /**
     * 设置背景遮罩 <0:默认值
     *
     * @return 0:不透明
     */
    protected float getDimAmount() {
        return 0.4f;
    }


    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        super.show(manager, tag);
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        if (DEBUG) {
            LogUtils.d(TAG, "onDismiss:");
        }
        if (onDismissListeners != null && onDismissListeners.size() > 0) {
            for (int i = 0; i < onDismissListeners.size(); i++) {
                OnDismissListener onDismissListener = onDismissListeners.get(i);
                if (onDismissListener != null) {
                    onDismissListener.onDismiss();
                }
            }
        }
        super.onDismiss(dialog);
    }



    /**
     * 关闭监听
     * @param onDismissListener
     */
    @Override
    public void addOnDismissListener(OnDismissListener onDismissListener) {
        if(onDismissListener==null) return;
        if (this.onDismissListeners == null) {
            onDismissListeners = new ArrayList<>();
        }
        if (!onDismissListeners.contains(onDismissListener)) {
            onDismissListeners.add(onDismissListener);
        }
    }

    @Override
    public void removeOnDismissListener(OnDismissListener onDismissListener) {
        if(onDismissListener==null) return;
        if(onDismissListeners!=null && onDismissListeners.size()>0){
            onDismissListeners.remove(onDismissListener);
        }
    }
}
